package com.drama.api.sys.message.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.drama.api.base.ApiIndex;
import com.drama.api.base.Constants;
import com.drama.api.base.R;
import com.drama.api.base.RestPage;
import com.drama.api.company.message.domain.vo.CompanyMessageReadVO;
import com.drama.api.company.message.domain.vo.CompanyMessageVO;
import com.drama.api.sys.message.domain.vo.SysMessageAllSearchVO;
import com.drama.api.sys.message.domain.vo.SysMessageSearchVO;
import com.drama.api.sys.message.domain.vo.SysMessageVO;
import com.drama.api.sys.message.service.ISysMessageService;
import com.drama.util.BindingResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api( tags = ApiIndex.SysMessageController)
@RequestMapping(value = "/api/sys/message")
@RestController
@SaCheckLogin
public class SysMessageController {

    @Autowired
    private ISysMessageService service;

    @ApiOperation(value = "查询信息列表【分页】")
    @GetMapping(value = "/list")
    public R<RestPage<List<SysMessageVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute SysMessageSearchVO searchVO) {
        List<CompanyMessageVO> list = service.getList(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @ApiOperation(value = "查询所有信息列表【不分页】")
    @GetMapping(value = "/all_list")
    public R<List<SysMessageVO>> all_list(
            @ApiParam(value = "查询参数") @ModelAttribute SysMessageAllSearchVO searchVO) {
        List<CompanyMessageVO> list = service.getAllList(searchVO);
        return new R(list);
    }



    @ApiOperation(value = "标记为已读")
    @PostMapping(value = "/read")
    public R read(@ApiParam(value = "参数") @Valid @RequestBody CompanyMessageReadVO vo, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.read(vo);
        }
        return result;
    }

    @ApiOperation(value = "全部标加为已读")
    @PostMapping(value = "/all_read")
    public R all_read() {
        R result= new R();
        service.all_read();
        return result;
    }

}
